<?php
class admin_categoryController extends adminController{

    //栏目添加
    public function add()
    {
        $this->view()->assign(api('admin', 'category_common_info'));
        $this->model_info=model('admin_category')->get_model();
        $this->show('admin_category/info');
    }

    //栏目保存
    public function add_save()
    {
        $model=model('admin_category')->get_model();
        $_POST['mid']=$model['mid'];
        $list = explode("\n", $_POST['namelist']);
        if(empty($list)){
            $this->msg('栏目名称未填写',0);
        }
        if(empty($_POST['class_tpl'])&&empty($_POST['content_tpl'])){
            $this->msg('栏目或内容模板未选择！',0);
        }
        foreach ($list as $name) {
            if(empty($name)){
                continue;
            }
            $name_array=explode('|',$name);
            if(empty($name_array)){
                continue;
            }
            $_POST['name']=$name_array[0];
            $_POST['urlname']=$name_array[1];
            $cid=api('admin', 'category_add',$_POST);
        }
        $this->msg('栏目添加成功！');
    }

    //栏目编辑
    public function edit()
    {
        $cid=intval($_GET['cid']);
        $this->alert_str($cid,'int');
        $this->view()->assign(api('admin', 'category_common_info',$cid));
        $this->model_info=model('admin_category')->get_model();
        $this->show('admin_category/info');
    }

    //栏目保存
    public function edit_save()
    {
        $data=in($_POST);
        $this->alert_str($data['cid'],'int',true);
        //检查
        if(empty($data['name'])){
            $this->msg('栏目名称未填写',0);
        }
        if(empty($data['class_tpl'])&&empty($data['content_tpl'])){
            $this->msg('栏目或内容模板未选择！',0);
        }
        
        // 分类检测
        if ($data['pid'] == $data['cid']){
            $this->msg('不可以将当前栏目设置为上一级栏目！',0);
            return;
        }
        $cat = api('admin', 'category_list',$data['cid']);
        if (!empty($cat)) {
            foreach ($cat as $vo) {
                if ($data['pid'] == $vo['cid']) {
                    $this->msg('不可以将上一级栏目移动到子栏目！',0);
                    return;
                }
            }
        }
        api('admin', 'category_edit',$data);
        $this->msg('栏目编辑成功！',1);
    }

    //栏目删除
    public function del()
    {
        $cid=intval($_POST['cid']);
        $this->alert_str($cid,'int',true);
        if(api('admin', 'category_list_count',$cid)){
            $this->msg('请先删除子栏目！',0);
        }
        api('admin', 'category_del',$cid);
        
        $list=api('admin','get_list_id',$cid);
        if($list){
            foreach ($list as $value) {
                api('admin', 'content_del',$value);
                model('admin_content')->del($value['aid']);
            }
        }

        $this->msg('栏目删除成功！',1);
    }
}